package com.netafull;

import java.util.List;

import com.netafull.adapter.NewsCommentAdapter;
import com.netafull.analyze.AnalyzeXMLForComments;
import com.netafull.analyze.Comment;
import com.netafull.util.CloseUtil;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NewsComments extends ShowAdActivity {
	ImageButton mNewsCommentBack;
	ImageButton mNewsCommentReply;
	
	int newsCommentsId;
	String newsTitle;
	String newsCommentsUrl;
	
	View progress;
	
	Handler handler;
	
	ListView listView;
	TextView textNull;
	NewsCommentAdapter adapter;
	List<Comment> list;
	AnalyzeXMLForComments analyzeXML;
	
	Thread t;
	
	boolean b = false;
	
	SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newscomments);
		
		show();
		
		sp = getSharedPreferences("netafull",Context.MODE_PRIVATE);
		
		CloseUtil.activityList.add(NewsComments.this);
		
		//listView = getListView();
		listView = (ListView) findViewById(R.id.newscomments_list);
		analyzeXML = new AnalyzeXMLForComments();
		
		progress = findViewById(R.id.list_progress);
		textNull = (TextView) findViewById(R.id.comments_null);
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		newsCommentsId = bundle.getInt("id");
		newsTitle = bundle.getString("title");
		newsCommentsUrl = getResources().getString(R.string.getcomment_url) + newsCommentsId+"&device=Android";
		
		mNewsCommentBack = (ImageButton) findViewById(R.id.newscomments_button_left);
		mNewsCommentReply = (ImageButton) findViewById(R.id.newscomments_button_right);
		mNewsCommentBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				NewsComments.this.finish();
			}
		});
		mNewsCommentReply.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				b = true;
				Intent intent = new Intent(NewsComments.this, NewsReply.class);
				Bundle bundle = new Bundle();
				bundle.putInt("id", newsCommentsId);
				bundle.putString("title", newsTitle);
				intent.putExtras(bundle);
				NewsComments.this.startActivityForResult(intent, 11);
			}
		});
		
		requestData();
		
		
	}
	public void requestData(){
		handler = new Handler(){
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case 1:
					progress.setVisibility(View.GONE);
					showList();// 显示数据内容
					break;
				case 0:
					progress.setVisibility(View.GONE);
					listView.setVisibility(View.GONE);
					textNull.setVisibility(View.VISIBLE);
					//showList();// 显示数据内容
					//Toast.makeText(NewsComments.this,R.string.null_comment, Toast.LENGTH_LONG).show();
					break;
				}
			};
		};
		t = new Thread(){
			public void run() {
				if(progress.VISIBLE!= View.VISIBLE){
					progress.setVisibility(View.VISIBLE);
				}
				System.out.println("开始请求评论");
				list = analyzeXML.getRss(newsCommentsUrl);
				Message msg = new Message();
				if (list.size() != 0) {
					msg.what = 1;
				} else {
					System.out.println("没有数据");
					msg.what = 0;
				}
				handler.sendMessage(msg);
			};
		};
		t.start();
	}
	// 显示列表
	private void showList() {
		adapter = new NewsCommentAdapter(this, list);
			
		//setListAdapter(adapter);
		listView.setAdapter(adapter);
		}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(t.isAlive()){
				t.interrupt();
				System.out.println("线程正在运行，强行中断");
			}
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		b = false;
		if(requestCode == 11){
			if(resultCode == 10){
				//Toast.makeText(this, R.string.send_comment_success, Toast.LENGTH_SHORT).show();
				progress.setVisibility(View.VISIBLE);
				listView.setVisibility(View.VISIBLE);
				textNull.setVisibility(View.GONE);
				requestData();
			} else if(resultCode == 20){
				//Toast.makeText(this, R.string.send_comment_failure, Toast.LENGTH_SHORT).show();
				progress.setVisibility(View.VISIBLE);
				requestData();
			}
		}
	}
	@Override
	protected void onUserLeaveHint() {
		// TODO Auto-generated method stub
		super.onUserLeaveHint();
		if(!b){
			if(sp!=null){
				Editor editor = sp.edit();
				editor.clear();
				editor.commit();
			}
			System.out.println("开始销毁Activity");
			CloseUtil.closeActivity();
		}else{
			System.out.println("没有销毁Activity");
		}
	}
	
}
